package com.blockmeta.bbs.minemodule.adapter;

import android.content.Context;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.net.callback.Callback;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.baselibrary.utils.gson.GsonUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.constant.NetConstant;
import com.blockmeta.bbs.businesslibrary.net.okhttp.OKHttpGetReq;
import com.blockmeta.bbs.businesslibrary.pojo.ActiveJoinPOJO;
import com.blockmeta.bbs.businesslibrary.pojo.EcodePOJO;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.minemodule.R;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.HashMap;
import java.util.List;

/**
 * Created by author on 2018/2/1.
 */

public class ActiveJoinAdapter extends BaseAdapter<ActiveJoinPOJO, BaseViewHolder> {
    private Context mContext;

    public ActiveJoinAdapter(int layoutResId, @Nullable List<ActiveJoinPOJO> data, Context context) {
        super(layoutResId, data);
        this.mContext = context;
    }


    @Override
    protected void convert(BaseViewHolder helper, final ActiveJoinPOJO item) {
        helper.setText(R.id.item_list_active_title, item.getSubject());
        View views = helper.getView(R.id.item_list_active_views);
        views.setVisibility(View.GONE);
        //  "verified": "0",是否审核,0:待审核;1:已通过;2:未通过待完善

        if (item.getActivity().getThumb() != null && item.getActivity().getThumb().startsWith("http")) {
            ImageView imageView = helper.getView(R.id.item_list_active_img);
            Glide.with(mContext).load(item.getActivity().getAttachurl()).placeholder(R.drawable.yoona).into(imageView);
        }
        if (!TextUtils.isEmpty(item.getActivity().getStarttimefrom())) {
            helper.setText(R.id.item_list_active_time, FormatUtil.getTimetoHMS(Integer.parseInt(item.getActivity().getStarttimefrom()), "yyyy-MM-dd HH:mm"));
        }

        if (!TextUtils.isEmpty(item.getActivity().getStarttimefrom())) {
            int i1 = Integer.parseInt(item.getActivity().getStarttimefrom());
            long l1 = i1 * 1000L;
            long l = System.currentTimeMillis();
            TextView view = helper.getView(R.id.item_active_bg);
            if (l1 < l) {
                view.setVisibility(View.VISIBLE);
            } else {
                view.setVisibility(View.GONE);
            }
        }


        helper.setText(R.id.item_list_active_location, item.getActivity().getPlace());

        TextView tvTag = helper.getView(R.id.item_list_active_money);
        if (item.getVerified().equals("0")) {
            tvTag.setTextColor(mContext.getResources().getColor(R.color.base_blue));
            tvTag.setText("待审核");
        } else if (item.getVerified().equals("1")) {
            tvTag.setTextColor(mContext.getResources().getColor(R.color.base_green));

                tvTag.setText("已通过");

        } else if (item.getVerified().equals("2")) {
            tvTag.setTextColor(mContext.getResources().getColor(R.color.base_color_red));
            tvTag.setText("未通过待完善");
        } else {
            tvTag.setTextColor(mContext.getResources().getColor(R.color.base_color_red));
            tvTag.setText("未通过");
        }

        final LinearLayout main = helper.getView(R.id.item_list_active_main);
        main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(item.getActivity().getThumb()))return;
                OKHttpGetReq okHttpGetReq = new OKHttpGetReq(mContext, NetConstant.SUBMIT_ACTIVE_FORM + "/" + item.getTid());
                HashMap<String, String> map = new HashMap<>();
                map.put("tid", item.getTid());
                map.put("applyid", item.getApplyid());
                okHttpGetReq.getDataWithParam(new Callback() {
                    @Override
                    public void onError(String resp) {
                        startToActivity(item);
                    }

                    @Override
                    public void onResponse(String resp, int id) {
                        try {
                            String result1 = resp.replace("[", "{");
                            String result2 = result1.replace("]", "}");
                            EcodePOJO ecodePOJO = GsonUtil.buildGson().fromJson(result2, EcodePOJO.class);
                            if (!TextUtils.isEmpty(ecodePOJO.getQrcode())) {
                                RouterHelper.startActivity(mContext,RouterPath.ERWEIMA_ACTIVITY,item.getTid(),item.getApplyid());
                            } else {
                                startToActivity(item);
                            }
                        } catch (Exception e) {
                            startToActivity(item);
                        }

                    }
                }, "uid", AccountHelper.getUid(), map);
            }
        });
    }

    private void startToActivity( ActiveJoinPOJO item) {
        RouterHelper.startActivity(mContext, RouterPath.ACTIVE_DETAIL_ACTIVITY,item.getTid());
    }
}
